/**
 * Copyright (C) 2011  Robin Dietrich, Nils Kenneweg, Leonard Stutz
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/ 

#ifndef DATEDCONTAINER_HPP
#define	DATEDCONTAINER_HPP

#include <ctime>
#include <string>
#include <boost/unordered_map.hpp>
#include <boost/shared_ptr.hpp>

#include "../config.hpp"

/** dated container class which removed ressources based on their access time */
template <class C, typename T> class datedContainer {    
    private:        
        /** value structure */
        struct val {
            //last accessed
            time_t accessed;
            //actual value
            boost::shared_ptr<C> obj;
        };
        
        //typedef to make iterating more readable
        typedef boost::unordered_map<T, datedContainer::val> dCon;
        
        /** structure mapping */
        dCon container;
    public:
        /** constructor */
        datedContainer() {};
        
        /** destructor */        
        virtual ~datedContainer() {
            typename dCon::iterator it;
            for (it = this->container.begin(); it != this->container.end(); ++it) {
                it->second.obj.reset();
            }
        };
        
        /** removes old entries from the container*/
        void cleanup() {           
            typename dCon::iterator it;
            for (it = container.begin(); it != container.end(); ++it) {
                if (it->second.accessed < (time(NULL)-P_BLOCKLIFETIME)) {
                    this->container.erase(it);
                }
            }
        }
        
        /** returns an entry by id */
        boost::shared_ptr<C> get(T id) {
            this->cleanup();
            if (this->has(id)) {
                this->container[id].accessed = time(NULL);
                return this->container[id].obj;
            }
        }
        
        /** sets an entry by id */
        void set(T id, C* obj) {
            this->container[id].obj.reset(obj);
            this->container[id].accessed = time(NULL);
        }
        
        /** checks if an entry exists */
        bool has(T id) {
            if (this->container.find(id) != this->container.end()) {
                return true;
            }
            
            return false;
        }
        
        /** deletes an entry by id */
        void del(T id) {
            if (this->has(id)) {
                delete this->container[id].obj;
                this->container.erase(id);
            }
        }
};


#endif	/* DATEDCONTAINER_HPP */

